package org.electrical.controller.functionality;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.electrical.dao.EleChatDao;
import org.electrical.pojo.EleChat;
import org.electrical.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/chat")
@CrossOrigin(value = "*")
public class EleChatController {

    @Autowired
    private EleChatDao eleChatDao;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<EleChat>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, String cusId) {

        Page<EleChat> pager = PageHelper.startPage(page, limit);
        List<EleChat> list = eleChatDao.getByCusId(cusId);
        return new ResponseResult(pager.getTotal(), list);

    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(EleChat eleChat) {

        eleChatDao.add(eleChat);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int chatId) {

        eleChatDao.delete(chatId);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(EleChat eleChat) {

        eleChatDao.update(eleChat);
        return ResponseResult.createSuccessResult();

    }

}
